/*
 * Copyright 2007 Ulf Ochsenfahrt <ulf@ofahrt.de>
 *
 * This file is part of mnemoj, a software transactional memory implementation
 * that is based on Project Darkstar.
 *
 * mnemoj is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation and
 * distributed hereunder to you.
 *
 * Project Darkstar Server is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.ofahrt.mnemoj.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.sun.sgs.app.TransactionException;


final class SerializationHelper
{

// private code
static byte[] serialize(Object obj)
{
	if (!(obj instanceof Serializable))
		throw new TransactionException("");
	try
	{
		ByteArrayOutputStream baout = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(baout);
		out.writeObject(obj);
		out.flush();
		return baout.toByteArray();
	}
	catch (IOException e)
	{ throw new RuntimeException(e); }
}

static Object deserialize(byte[] data)
{
	try
	{
		ByteArrayInputStream bain = new ByteArrayInputStream(data);
		ObjectInputStream in = new ObjectInputStream(bain);
		return in.readObject();
	}
	catch (IOException e)
	{ throw new RuntimeException(e); }
	catch (ClassNotFoundException e)
	{ throw new RuntimeException(e); }
}

}
